/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Beasiswa;
import Entity.JenisInstansi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import library.Koneksi;

/**
 *
 * @author user
 */
public class DaftarBeasiswa {

    Beasiswa Beasiswa;

    //method untuk menambah beasiswa
    public boolean tambahBeasiswa(Beasiswa Beasiswa) {
        boolean hasil = false;
        this.Beasiswa = Beasiswa;
        Koneksi dbConn = new Koneksi();
        String sql = "insert into beasiswa(JUDUL_BEA,DESKRIPSI_BEA,DEADLINE,KUOT_BEA)"
                + " value("
                + "'" + this.Beasiswa.getNamaBeasiswa() + "',"
                + "'" + this.Beasiswa.getDeskripsiBeasiswa() + "',"
                + "'" + this.Beasiswa.getDeadline() + "',"
                + "'" + this.Beasiswa.getKuotaBeasiswa() + "'"
                + ")";
        System.out.println("sql : " + sql);
        PreparedStatement st = null;
        try {
            Connection conn = Koneksi.getKoneksi();
            st = conn.prepareStatement(sql);
            int rs = st.executeUpdate(sql);
            if (rs > 0) {
                hasil = true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("error : " + ex.getErrorCode());
            System.out.println("error : " + ex.getMessage());
        } finally {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return hasil;
    }

    //method untuk mengambil data daftar beasiswa
    public List<Beasiswa> ambilDaftarBeasiswa() {
        List<Beasiswa> daftarBeasiswa = new ArrayList<Beasiswa>();
        Koneksi dbConn = new Koneksi();
        String sql = "SELECT * FROM beasiswa";

        PreparedStatement st = null;
        ResultSet rs = null;
        try {

            Connection conn = Koneksi.getKoneksi();

            st = conn.prepareStatement(sql);
            rs = st.executeQuery();

            while (rs.next()) {
                this.Beasiswa = new Beasiswa();
                Beasiswa Beasiswa = new Beasiswa();
                Beasiswa.setIdBeasiswa(rs.getInt("ID_BEA"));
                Beasiswa.setNamaBeasiswa(rs.getString("JUDUL_BEA"));
                Beasiswa.setDeskripsiBeasiswa(rs.getString("DESKRIPSI_BEA"));
                Beasiswa.setDeadline(rs.getString("DEADLINE"));
                Beasiswa.setKuotaBeasiswa(rs.getString("KUOT_BEA"));
                daftarBeasiswa.add(Beasiswa);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return daftarBeasiswa;
    }

    public List<Beasiswa> ambilPengumumanBeasiswa() {
        List<Beasiswa> daftarBeasiswa = new ArrayList<Beasiswa>();
        Koneksi dbConn = new Koneksi();

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        int Month = c.get(Calendar.MONTH) + 1;
        String tanggal = c.get(Calendar.YEAR) + "-" + Month + "-" + c.get(Calendar.DATE);

        String sql = "SELECT * FROM beasiswa where DEADLINE > '"+tanggal+"'";

        PreparedStatement st = null;
        ResultSet rs = null;
        try {

            Connection conn = Koneksi.getKoneksi();

            st = conn.prepareStatement(sql);
            rs = st.executeQuery();

            while (rs.next()) {
                this.Beasiswa = new Beasiswa();
                Beasiswa Beasiswa = new Beasiswa();
                Beasiswa.setIdBeasiswa(rs.getInt("ID_BEA"));
                Beasiswa.setNamaBeasiswa(rs.getString("JUDUL_BEA"));
                Beasiswa.setDeskripsiBeasiswa(rs.getString("DESKRIPSI_BEA"));
                Beasiswa.setDeadline(rs.getString("DEADLINE"));
                Beasiswa.setKuotaBeasiswa(rs.getString("KUOT_BEA"));
                daftarBeasiswa.add(Beasiswa);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return daftarBeasiswa;
    }
    //method untuk menghapus data beasiswa

    public boolean hapusBeasiswa(Beasiswa beasiswa) {

        boolean hasil = true;

        PreparedStatement st = null;
        Koneksi dbConn = new Koneksi();

        String sql = "DELETE FROM beasiswa WHERE ID_BEA = " + beasiswa.getIdBeasiswa();

        Connection conn = Koneksi.getKoneksi();
        try {
            st = conn.prepareStatement(sql);
            int rs = st.executeUpdate(sql);
            if (rs > 0) {
                hasil = true;
            } else {
                hasil = false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return hasil;

    }

    //method untuk mengambil data beasiswa dengan id tertentu
    public Beasiswa ambilDataBeasiswa(String idBeasiswa) {

        Koneksi dbConn = new Koneksi();
        String sql = "SELECT * FROM beasiswa as A, beasiswa as B where A.ID_BEA = B.ID_BEA and A.ID_BEA=" + idBeasiswa;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {

            Connection conn = Koneksi.getKoneksi();

            st = conn.prepareStatement(sql);
            rs = st.executeQuery();

            while (rs.next()) {
                this.Beasiswa = new Beasiswa();
                Beasiswa.setIdBeasiswa(rs.getInt("ID_BEA"));
                Beasiswa.setNamaBeasiswa(rs.getString("JUDUL_BEA"));
                Beasiswa.setDeskripsiBeasiswa(rs.getString("DESKRIPSI_BEA"));
                Beasiswa.setKuotaBeasiswa(rs.getString("KUOT_BEA"));
                Beasiswa.setDeadline(rs.getString("DEADLINE"));

            }
        } catch (SQLException ex) {
            Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return Beasiswa;

    }

    //   method untuk mengedit beasiswa
    public boolean editBeasiswa(Beasiswa beasiswa, Beasiswa beasiswaLama) {
        boolean hasil = false;
        this.Beasiswa = beasiswa;
        Koneksi dbConn = new Koneksi();

        String sql = "update beasiswa SET "
                + "JUDUL_BEA = '" + this.Beasiswa.getNamaBeasiswa()
                + "', DESKRIPSI_BEA = '" + this.Beasiswa.getDeskripsiBeasiswa()
                + "', DEADLINE = '" + this.Beasiswa.getDeadline()
                + "', KUOT_BEA = '" + this.Beasiswa.getKuotaBeasiswa()
                + "' where ID_BEA = " + beasiswaLama.getIdBeasiswa();



        PreparedStatement st = null;

        try {
            Connection conn = Koneksi.getKoneksi();
            st = conn.prepareStatement(sql);
            int rs = st.executeUpdate(sql);
            if (rs > 0) {
                hasil = true;
            } else {
                hasil = false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaftarBeasiswa.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return hasil;
    }
}
